/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;

import soccerscope.db.DatabaseReader;
import soccerscope.db.GameItem;

public class LoadGameDialog extends JDialog {
    public LoadGameDialog() {
        setSelectedGame(new GameItem(-1, null));
        loadGameName();

        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void setSelectedGame(GameItem selectedGame) {
        this.selectedGame = selectedGame;
    }

    public GameItem getSelectedGame() {
        return selectedGame;
    }

    ArrayList <GameItem> gis;
    JScrollPane jScrollPane1 = new JScrollPane();
    JList jGameList = new JList();
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel1 = new JPanel();
    JButton jCancelButton = new JButton();
    JButton jDeleteButton = new JButton();
    JButton jLoadButton = new JButton();
    private void jbInit() throws Exception {
        jGameList.setModel(gameItems);
        jLoadButton.addActionListener(new LoadGameDialog_jLoadButton_actionAdapter(this));
        this.setModal(true);
        jCancelButton.addActionListener(new LoadGameDialog_jCancelButton_actionAdapter(this));
        jScrollPane1.getViewport().add(jGameList);
        this.getContentPane().setLayout(borderLayout1);
        this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
        jCancelButton.setText("Cancel");
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
        jDeleteButton.setText("Delete");
        jLoadButton.setText("Load");
        jPanel1.add(jLoadButton);
        jPanel1.add(jDeleteButton);
        jPanel1.add(jCancelButton);
        this.setBounds(150, 150, 450, 350);
        this.setTitle("Load game");
    }

    void loadGameName()
    {
        gis = DatabaseReader.getInstance().getGameInfos();
        for (GameItem i : gis)
            gameItems.addElement(i.getDescription());

    }

    private DefaultListModel gameItems = new DefaultListModel();
    public void jLoadButton_actionPerformed(ActionEvent e) {
        setSelectedGame( gis.get(jGameList.getSelectedIndex()));
        this.dispose();
    }

    public void jCancelButton_actionPerformed(ActionEvent e) {
        setSelectedGame(new GameItem(-1, null));
        this.dispose();
    }

    private GameItem selectedGame;
}


class LoadGameDialog_jCancelButton_actionAdapter implements ActionListener {
    private LoadGameDialog adaptee;
    LoadGameDialog_jCancelButton_actionAdapter(LoadGameDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jCancelButton_actionPerformed(e);
    }
}


class LoadGameDialog_jLoadButton_actionAdapter implements ActionListener {
    private LoadGameDialog adaptee;
    LoadGameDialog_jLoadButton_actionAdapter(LoadGameDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jLoadButton_actionPerformed(e);
    }
}
